package com.ken.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class textUtil {
    public static String returnText(String json) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode rootNode = objectMapper.readTree(json);

        StringBuilder allContent = new StringBuilder();

        // 遍历pages -> lines -> words 获取content并拼接
        if (rootNode.has("pages")) {
            for (JsonNode pageNode : rootNode.get("pages")) {
                if (pageNode.has("lines")) {
                    for (JsonNode lineNode : pageNode.get("lines")) {
                        if (lineNode.has("words")) {
                            for (JsonNode wordNode : lineNode.get("words")) {
                                if (wordNode.has("content")) {
                                    allContent.append(wordNode.get("content").asText()).append(" ");
                                }
                            }
                        }
                    }
                }
            }
        }
        String concatenatedContent = allContent.toString().trim();
        return concatenatedContent;
    }
}
